﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Kaixin.House.Ranch
{
    using Kaixin.Tools;

    public class Product
    {
        private Network _network;
        private string _uid;           // your uid of this 
        private Friend _friend = null; // friend (owner) of this product
        private string _verify;        // verify code 

        public string Name;
        public string SKey;
        public int Num = 0;
        public int StealNum = 0;

        public bool Theived = false;

        public Product(string uid, Friend friend, string verify,
            Network network)
        {
            _uid = uid;
            _friend = friend;
            _verify = verify;
            _network = network;
        }

        public bool CanHarvest()
        {
            return !Theived;
        }

        public int Harvest()
        {
            string url = Consts.RootURL + Consts.RanchHarvestUrl;
            string data = "fuid=" + _friend.Uid +"&skey="+ SKey + "&verify=" + _verify;

            try
            {
                string respXML = _network.PostData(url, Encoding.ASCII.GetBytes(data));
                XMLParser parser = new XMLParser(respXML);
                if ("succ".Equals(parser.GetNodeValue("/data/ret")))
                {
                    TextOutput.PrintLine("Harvest: " + _friend.Name + " "
                        + parser.GetNodeValue("/data/num") + " " + Name + " succ");
                    return 1;
                }
                else
                {
                    TextOutput.PrintLine("Harvest: " + _friend.Name + " "
                        + Name + "Fail. " + parser.GetNodeValue("/data/reason"));
                    return 0;
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}
